﻿using System.Windows;
using System.Windows.Controls;
using TemplateGenerator.Entities;

namespace TemplateGenerator.Controls 
{
	/// <summary>
	/// Interaction logic for GenerateTemplateControl.xaml
	/// </summary>
	public partial class GenerateTemplateControl : UserControl 
    {
		public GenerateTemplateControl() 
        {
			InitializeComponent();

            //set datacontext to the template list generated at startup
			TemplateView.DataContext = GeneratorHandler.CustomTemplates;
		}

		private void ButtonClick(object sender, RoutedEventArgs e) 
        {
			e.Handled = true;

            //gets button pressed and puts the datacontext in a custom template instance
			CustomTemplate template = (CustomTemplate)((Button)sender).DataContext;

            //generates new document based on the selected template
			GeneratorHandler.GenerateNewDocument(template.Name);

            //closes window when finished
			CloseParent();
		}

        //gives the control a property of the same type as the window. so that this control can close the parent window
		public static readonly DependencyProperty ParentWindowProperty =
			DependencyProperty.Register("ParentWindow", typeof (GenerateTemplateWindow), typeof (GenerateTemplateControl), new PropertyMetadata(default(GenerateTemplateWindow)));

        //getter/setter for the parentwindow property
		public GenerateTemplateWindow ParentWindow
		{
			get { return (GenerateTemplateWindow) GetValue(ParentWindowProperty); }
			set { SetValue(ParentWindowProperty, value); }
		}
		
        //closes the parent window in which this control is resides
		private void CloseParent() 
        {
			if (ParentWindow != null) ParentWindow.Close();
		}

	}
}
